package com.fosu.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fosu.common.ResponseResult;
import com.fosu.dao.CampusDao;
import com.fosu.dao.CategoriesDao;
import com.fosu.dao.ProductDao;
import com.fosu.domain.Campus;
import com.fosu.domain.Product;
import com.fosu.domain.ProductCategories;
import com.fosu.service.ProductService;
import com.fosu.vo.MiniProduct;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Desription
 * 商品业务实现类
 *
 * @author manno
 * @date 2020/10/19
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private CampusDao campusDao;
    @Autowired
    private CategoriesDao categoriesDao;


    /**
     * 分页获取最新商品
     *
     * @param resp        Servlet响应对象
     * @param currentPage 当前页数
     * @param pageSize    页面大小
     * @return ResponseResult
     */
    @Override
    public ResponseResult getProducts(HttpServletResponse resp, int currentPage, int pageSize) {
        currentPage = (currentPage - 1) * pageSize;
        List<MiniProduct> products = productDao.getProducts(currentPage, pageSize);
        if (products.size() <= 0) {
            return ResponseResult.build(resp, 500, 10001, "没有更多页了", products);
        }
        return ResponseResult.build(resp, 200, 10000, "成功获取商品信息", products);
    }

    @Override
    public ResponseResult findProductById(HttpServletResponse resp, int productId) {
        /**之后要加上判空异常处理*/
        Product product = productDao.findProductById(productId);
        if (product != null) {
            return ResponseResult.build(resp, 200, 10019, "按商品id获取商品成功", product);
        }
        return ResponseResult.build(resp, 500, 10006, "按商品id获取商品失败", null);
    }

    @Override
    public List<MiniProduct> getProductsByIds(List<Integer> productIds) {
        /**之后要加上判空异常处理*/
        return productDao.getProductsByIds(productIds);
    }


    @Override
    public ResponseResult getProductsByCampusAndCategories(HttpServletResponse resp, JSONObject jsonObject) throws Exception
    {
        /**
         * @param currentPage 当前页数 为空默认第一页
         * @param pageSize 页面大小 为空默认15
         * @param campusId 校区id
         * @param categoriesId 分类id
         * */
        int currentPage = jsonObject.getIntValue("currPage")==0?1:jsonObject.getIntValue("currPage");
        int pageSize = jsonObject.getIntValue("pageSize")==0?15:jsonObject.getIntValue("pageSize");
        JSONArray campusIds = null;
        JSONArray categoriesIds = null;
        int categoriesId = 0;
        int campusId = 0;
        try{
            categoriesIds = jsonObject.getJSONArray("categoriesId");
        }catch (ClassCastException e)
        {
            try{
//                    e.printStackTrace();
                /**如果传过来的分类只有一个就用Int*/
                categoriesId = jsonObject.getIntValue("categoriesId");
            }catch (ClassCastException e1)
            {
                /**如果还不符合预期的分类类型就报错*/
                e1.printStackTrace();
                return ResponseResult.build(resp,500,30012,"分类数据不符合规范",null);
            }

        }
        try{
            campusIds = jsonObject.getJSONArray("campusId");
        }catch (ClassCastException e)
        {
            try
            {
                /**如果传过来的校区只有一个就用Int*/
                campusId = jsonObject.getIntValue("campusId");
            }
            catch(ClassCastException e1)
            {
                /**如果还不符合预期的校区类型就报错*/
                e1.printStackTrace();
                return ResponseResult.build(resp,500,30012,"校区数据不符合规范",null);
            }
        }

        /**参数判空异常处理
         * 若campusId和categoriesId都为空则相当于selectALL*/
        if (campusIds == null && categoriesIds == null) {
            return ResponseResult.ok(resp, productDao.getProducts(currentPage, pageSize));
        }

        List<String> categoriesIdStr = new ArrayList<>();
        /**部分参数判空处理*/
        /**如果只选了一个*/
        if (categoriesIds != null) {
            for(int i = 0 ; i < categoriesIds.size() ; i++)
            {
                categoriesIdStr.add(categoriesIds.get(i)+"");
            }
        }
        /**如果一个都没选*/
        else if(categoriesId != 0)
        {
            categoriesIdStr.add(categoriesId+"");
        }
        /**就选择全部*/
        else {
            List<ProductCategories> allCategories = categoriesDao.findAll();
            for (ProductCategories c : allCategories) {
                categoriesIdStr.add(c.getCategoriesId() + "");
            }
        }
        System.out.println(categoriesIdStr);
        List<String> campusIdStr = new ArrayList<>();
        /**部分参数判空处理*/
        /**如果只选了一个*/
        if (campusIds != null) {
            for(int i = 0 ; i < campusIds.size() ; i++)
            {
                campusIdStr.add(campusIds.get(i)+"");
            }
        }
        /**如果一个都没选*/
        else if(campusId != 0)
        {
            campusIdStr.add(campusId+"");
        }
        /**就选择全部*/
        else {
            List<Campus> allCampus = campusDao.findAll();
            for (Campus c : allCampus) {
                campusIdStr.add(c.getCampusId() + "");
            }
        }
        System.out.println(campusIdStr);
        currentPage = (currentPage - 1) * pageSize;
        List<MiniProduct> products = productDao.getProductsByCampusAndCategories(currentPage, pageSize, campusIdStr,categoriesIdStr);
        if (products.size() <= 0) {
            return ResponseResult.build(resp, 500, 10001, "没有更多页了", products);
        }
        return ResponseResult.build(resp, 200, 10000, "成功获取商品信息", products);
    }


    /**
     * 搜索功能
     */
    @Override
    public ResponseResult findMsgBySearch(HttpServletResponse resp,JSONObject jsonPram ) throws Exception {
        JSONObject jsonObject = null;
        String searchInfo = null;
        Integer currPage = null;
        Integer pageSize = null;
        List<MiniProduct> findMsgByCategories = new ArrayList<>();
        List<MiniProduct> findMsgByCampus = new ArrayList<>();
        List<MiniProduct> findMsgBySearch = new ArrayList<>();
        List<MiniProduct> findMsgByCustomizeCategories = new ArrayList<>();
        try {
            jsonObject = new JSONObject();
            searchInfo = jsonPram.getString("searchInfo");
            currPage = jsonPram.getInteger("currPage");
            pageSize = jsonPram.getInteger("pageSize");
            /**输入检验*/
            if(searchInfo == null)
            {
                return ResponseResult.fail(resp,"搜索信息不能为空！");
            }
            /**没有指定页数默认为1*/
            if(currPage == null)
            {
                currPage = 1;
            }
            /**没有指定页大小默认为5*/
            if(pageSize == null)
            {
                pageSize = 5;
            }
            long begin = System.currentTimeMillis();
            long end = System.currentTimeMillis();
            long segmentBegin = System.currentTimeMillis();
            int i = 0;
            /**如果输入的搜索字串长度大于1*/
            if (searchInfo.length() > 1) {
                /**根据输入的字符串查找(用户自定义字串部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                A:
                for (int j = searchInfo.length() - 1; j > i; j--) {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgBySearch(searchInfo.substring(i, j),currPage,pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgBySearch.size() < pageSize)
                    {
                        for (Iterator<MiniProduct> iterator = temp.iterator(); iterator.hasNext(); )
                        {
                            MiniProduct p = iterator.next();
                            if (findMsgBySearch.size() < pageSize)
                            {

                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgBySearch.size(); z++)
                                {
                                    if (p.getProductId().equals(findMsgBySearch.get(z).getProductId()))
                                    {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat)
                                {
                                    findMsgBySearch.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else {
                                break A;
                            }
                        }
                    } else if (j > i + 1) {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgBySearch(searchInfo.substring(++i, j),currPage,pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgBySearch.size() < pageSize) {
                            for (MiniProduct p : temp2) {
                                if (findMsgBySearch.size() < pageSize) {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgBySearch.size(); z++) {
                                        if (p.getProductId() == findMsgBySearch.get(z).getProductId()) {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat) {
                                        findMsgBySearch.add(p);
                                    }
                                    findMsgBySearch.add(p);
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else {
                                    break A;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgBySearch.size() >= pageSize) {
                        break A;
                    }

                }
                end = System.currentTimeMillis();
                System.out.println("本次跟据自定义字串查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
                System.out.println(findMsgBySearch);
                segmentBegin = System.currentTimeMillis();

                /**根据输入的字符串查找(分类部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                i = 0;
                B:
                for (int j = searchInfo.length() - 1; j > i; j--) {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgByCategories(searchInfo.substring(i, j),currPage,pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgByCategories.size() < pageSize) {
                        for (MiniProduct p : temp) {
                            if (findMsgByCategories.size() < pageSize) {
                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgByCategories.size(); z++) {
                                    if (p.getProductId() == findMsgByCategories.get(z).getProductId()) {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat) {
                                    findMsgByCategories.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else {
                                break B;
                            }
                        }
                    } else if (j > i + 1) {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgByCategories(searchInfo.substring(++i, j),currPage,pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgByCategories.size() < pageSize) {
                            for (MiniProduct p : temp2) {
                                if (findMsgByCategories.size() < pageSize) {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgByCategories.size(); z++) {
                                        if (p.getProductId() == findMsgByCategories.get(z).getProductId()) {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat) {
                                        findMsgByCategories.add(p);
                                    }
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else {
                                    break B;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgByCategories.size() >= pageSize)
                    {
                        break;
                    }
                }


                end = System.currentTimeMillis();
                System.out.println("本次跟据分类查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
                System.out.println(findMsgByCategories);
                segmentBegin = System.currentTimeMillis();
                /**根据输入的字符串查找(校区部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                i = 0;
                C:
                for (int j = searchInfo.length() - 1; j > i; j--) {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgByCampus(searchInfo.substring(i, j),currPage,pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgByCampus.size() < pageSize) {
                        for (MiniProduct p : temp) {
                            if (findMsgByCampus.size() < pageSize) {
                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgByCampus.size(); z++) {
                                    if (p.getProductId() == findMsgByCampus.get(z).getProductId()) {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat) {
                                    findMsgByCampus.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else {
                                break C;
                            }
                        }
                    } else if (j > i + 1) {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgByCampus(searchInfo.substring(++i, j),currPage,pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgByCampus.size() < pageSize) {
                            for (MiniProduct p : temp2) {
                                if (findMsgByCampus.size() < pageSize) {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgByCampus.size(); z++) {
                                        if (p.getProductId() == findMsgByCampus.get(z).getProductId()) {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat) {
                                        findMsgByCampus.add(p);
                                    }
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else {
                                    break C;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgByCampus.size() >= pageSize)
                    {
                        break;
                    }
                }
                end = System.currentTimeMillis();
                System.out.println("本次跟据校区查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
                System.out.println(findMsgByCampus);
                segmentBegin = System.currentTimeMillis();
                /**根据输入的字符串查找(商品自定义分类部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                i = 0;
                D:
                for (int j = searchInfo.length() - 1; j > i; j--) {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgByCustomizeCategories(searchInfo.substring(i, j),currPage,pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgByCustomizeCategories.size() < pageSize) {
                        for (MiniProduct p : temp) {
                            if (findMsgByCustomizeCategories.size() < pageSize) {
                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgByCustomizeCategories.size(); z++) {
                                    if (p.getProductId() == findMsgByCustomizeCategories.get(z).getProductId()) {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat) {
                                    findMsgByCustomizeCategories.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else {
                                break D;
                            }
                        }
                    } else if (j > i + 1) {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgByCustomizeCategories(searchInfo.substring(++i, j),currPage,pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgByCustomizeCategories.size() < pageSize) {
                            for (MiniProduct p : temp2) {
                                if (findMsgByCustomizeCategories.size() < pageSize) {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgByCustomizeCategories.size(); z++) {
                                        if (p.getProductId() == findMsgByCustomizeCategories.get(z).getProductId()) {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat) {
                                        findMsgByCustomizeCategories.add(p);
                                    }
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else {
                                    break D;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgByCustomizeCategories.size() >= pageSize) {
                        break D;
                    }


                }
            }
            /**如果用户只输入了一个字符就不用拆分了*/
            else {
                findMsgBySearch = productDao.findMsgBySearch(searchInfo,currPage,pageSize);
                findMsgByCategories = productDao.findMsgByCategories(searchInfo,currPage,pageSize);
                findMsgByCampus = productDao.findMsgByCampus(searchInfo,currPage,pageSize);
                findMsgByCustomizeCategories = productDao.findMsgByCustomizeCategories(searchInfo,currPage,pageSize);
            }
            end = System.currentTimeMillis();
            System.out.println("本次跟据自定义分类查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
            System.out.println(findMsgByCustomizeCategories);
            end = System.currentTimeMillis();
            System.out.println("本次查询用时：" + (end - begin) / 1000 + "." + (end - begin) % 1000 + "s");

            jsonObject.put("findMsgBySearch", findMsgBySearch);
            jsonObject.put("findMsgByCategories", findMsgByCategories);
            jsonObject.put("findMsgByCampus", findMsgByCampus);
            jsonObject.put("findMsgByCustomizeCategories", findMsgByCustomizeCategories);
            if (findMsgByCustomizeCategories.size() == 0 && findMsgBySearch.size() == 0 && findMsgByCampus.size() == 0 && findMsgByCategories.size() == 0) {
                return ResponseResult.build(resp, 500, 10005, "商品搜索结果为空,未查询到" + searchInfo + "的相关数据", jsonObject);
            }

            return ResponseResult.build(resp, 200, 10004, "成功搜索商品", jsonObject);
        } catch (
                Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //回滚事务
            return ResponseResult.fail(resp, "findMsgBySearch内部异常");
        }

    }

    @Override
    public ResponseResult findMoreAboutMsg(HttpServletResponse resp, JSONObject jsonPram) throws Exception
    {
        JSONObject jsonObject = null;
        String searchInfo = null;
        Integer currPage = null;
        Integer pageSize = null;
        List<MiniProduct> findMsgBySearch = new ArrayList<>();
        try {
            jsonObject = new JSONObject();
            searchInfo = jsonPram.getString("searchInfo");
            currPage = jsonPram.getInteger("currPage");
            pageSize = jsonPram.getInteger("pageSize");
            /**输入检验*/
            if(searchInfo == null)
            {
                return ResponseResult.fail(resp,"搜索信息不能为空！");
            }
            /**没有指定页数默认为1*/
            if(currPage == null)
            {
                currPage = 1;
            }
            /**没有指定页大小默认为5*/
            if(pageSize == null)
            {
                pageSize = 5;
            }
            long begin = System.currentTimeMillis();
            long end = System.currentTimeMillis();
            long segmentBegin = System.currentTimeMillis();
            int i = 0;
            /**如果输入的搜索字串长度大于1*/
            if (searchInfo.length() > 1)
            {
                /**根据输入的字符串查找(用户自定义字串部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                A:
                for (int j = searchInfo.length() - 1; j > i; j--)
                {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgBySearch(searchInfo.substring(i, j), currPage, pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgBySearch.size() < pageSize)
                    {
                        for (Iterator<MiniProduct> iterator = temp.iterator(); iterator.hasNext(); )
                        {
                            MiniProduct p = iterator.next();
                            if (findMsgBySearch.size() < pageSize)
                            {

                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgBySearch.size(); z++)
                                {
                                    if (p.getProductId().equals(findMsgBySearch.get(z).getProductId()))
                                    {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat)
                                {
                                    findMsgBySearch.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else
                            {
                                break A;
                            }
                        }
                    } else if (j > i + 1)
                    {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgBySearch(searchInfo.substring(++i, j), currPage, pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgBySearch.size() < pageSize)
                        {
                            for (MiniProduct p : temp2)
                            {
                                if (findMsgBySearch.size() < pageSize)
                                {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgBySearch.size(); z++)
                                    {
                                        if (p.getProductId() == findMsgBySearch.get(z).getProductId())
                                        {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat)
                                    {
                                        findMsgBySearch.add(p);
                                    }
                                    findMsgBySearch.add(p);
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else
                                {
                                    break A;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else
                        {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgBySearch.size() >= pageSize)
                    {
                        break A;
                    }

                }
            }
            /**如果用户只输入了一个字符就不用拆分了*/
            else {
                    findMsgBySearch = productDao.findMsgBySearch(searchInfo,currPage,pageSize);
                }
                end = System.currentTimeMillis();
                System.out.println("本次跟据自定义字串查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
                System.out.println(findMsgBySearch);
                jsonObject.put("findMsgBySearch", findMsgBySearch);
            return ResponseResult.build(resp, 200, 10004, "成功搜索商品", jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //回滚事务
            return ResponseResult.fail(resp, "findMoreAboutMsg内部异常");
        }

    }

    @Override
    public ResponseResult findMoreAboutCampus(HttpServletResponse resp, JSONObject jsonPram) throws Exception
    {
        JSONObject jsonObject = null;
        String searchInfo = null;
        Integer currPage = null;
        Integer pageSize = null;
        List<MiniProduct> findMsgByCampus = new ArrayList<>();
        try {
            jsonObject = new JSONObject();
            searchInfo = jsonPram.getString("searchInfo");
            currPage = jsonPram.getInteger("currPage");
            pageSize = jsonPram.getInteger("pageSize");
            /**输入检验*/
            if(searchInfo == null)
            {
                return ResponseResult.fail(resp,"搜索信息不能为空！");
            }
            /**没有指定页数默认为1*/
            if(currPage == null)
            {
                currPage = 1;
            }
            /**没有指定页大小默认为5*/
            if(pageSize == null)
            {
                pageSize = 5;
            }
            long begin = System.currentTimeMillis();
            long end = System.currentTimeMillis();
            long segmentBegin = System.currentTimeMillis();
            int i = 0;
            /**如果输入的搜索字串长度大于1*/
            if (searchInfo.length() > 1)
            {
                /**根据输入的字符串查找(用户自定义字串部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                A:
                for (int j = searchInfo.length() - 1; j > i; j--)
                {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgByCampus(searchInfo.substring(i, j), currPage, pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgByCampus.size() < pageSize)
                    {
                        for (Iterator<MiniProduct> iterator = temp.iterator(); iterator.hasNext(); )
                        {
                            MiniProduct p = iterator.next();
                            if (findMsgByCampus.size() < pageSize)
                            {

                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgByCampus.size(); z++)
                                {
                                    if (p.getProductId().equals(findMsgByCampus.get(z).getProductId()))
                                    {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat)
                                {
                                    findMsgByCampus.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else
                            {
                                break A;
                            }
                        }
                    } else if (j > i + 1)
                    {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgByCampus(searchInfo.substring(++i, j), currPage, pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgByCampus.size() < pageSize)
                        {
                            for (MiniProduct p : temp2)
                            {
                                if (findMsgByCampus.size() < pageSize)
                                {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgByCampus.size(); z++)
                                    {
                                        if (p.getProductId() == findMsgByCampus.get(z).getProductId())
                                        {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat)
                                    {
                                        findMsgByCampus.add(p);
                                    }
                                    findMsgByCampus.add(p);
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else
                                {
                                    break A;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else
                        {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgByCampus.size() >= pageSize)
                    {
                        break A;
                    }

                }
            }
            /**如果用户只输入了一个字符就不用拆分了*/
            else {
                findMsgByCampus = productDao.findMsgByCampus(searchInfo,currPage,pageSize);
            }
            end = System.currentTimeMillis();
            System.out.println("本次跟据校区查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
            System.out.println(findMsgByCampus);
            jsonObject.put("findMsgByCampus", findMsgByCampus);
            return ResponseResult.build(resp, 200, 10004, "成功搜索商品", jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //回滚事务
            return ResponseResult.fail(resp, "findMoreAboutCampus内部异常");
        }
    }

    @Override
    public ResponseResult findMoreAboutCategories(HttpServletResponse resp, JSONObject jsonPram) throws Exception
    {
        JSONObject jsonObject = null;
        String searchInfo = null;
        Integer currPage = null;
        Integer pageSize = null;
        List<MiniProduct> findMsgByCategories = new ArrayList<>();
        try {
            jsonObject = new JSONObject();
            searchInfo = jsonPram.getString("searchInfo");
            currPage = jsonPram.getInteger("currPage");
            pageSize = jsonPram.getInteger("pageSize");
            /**输入检验*/
            if(searchInfo == null)
            {
                return ResponseResult.fail(resp,"搜索信息不能为空！");
            }
            /**没有指定页数默认为1*/
            if(currPage == null)
            {
                currPage = 1;
            }
            /**没有指定页大小默认为5*/
            if(pageSize == null)
            {
                pageSize = 5;
            }
            long begin = System.currentTimeMillis();
            long end = System.currentTimeMillis();
            long segmentBegin = System.currentTimeMillis();
            int i = 0;
            /**如果输入的搜索字串长度大于1*/
            if (searchInfo.length() > 1)
            {
                /**根据输入的字符串查找(用户自定义字串部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                A:
                for (int j = searchInfo.length() - 1; j > i; j--)
                {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgByCategories(searchInfo.substring(i, j), currPage, pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgByCategories.size() < pageSize)
                    {
                        for (Iterator<MiniProduct> iterator = temp.iterator(); iterator.hasNext(); )
                        {
                            MiniProduct p = iterator.next();
                            if (findMsgByCategories.size() < pageSize)
                            {

                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgByCategories.size(); z++)
                                {
                                    if (p.getProductId().equals(findMsgByCategories.get(z).getProductId()))
                                    {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat)
                                {
                                    findMsgByCategories.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else
                            {
                                break A;
                            }
                        }
                    } else if (j > i + 1)
                    {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgByCategories(searchInfo.substring(++i, j), currPage, pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgByCategories.size() < pageSize)
                        {
                            for (MiniProduct p : temp2)
                            {
                                if (findMsgByCategories.size() < pageSize)
                                {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgByCategories.size(); z++)
                                    {
                                        if (p.getProductId() == findMsgByCategories.get(z).getProductId())
                                        {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat)
                                    {
                                        findMsgByCategories.add(p);
                                    }
                                    findMsgByCategories.add(p);
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else
                                {
                                    break A;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else
                        {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgByCategories.size() >= pageSize)
                    {
                        break A;
                    }

                }
            }
            /**如果用户只输入了一个字符就不用拆分了*/
            else {
                findMsgByCategories = productDao.findMsgByCategories(searchInfo,currPage,pageSize);
            }
            end = System.currentTimeMillis();
            System.out.println("本次跟据分类查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
            System.out.println(findMsgByCategories);
            jsonObject.put("findMsgByCategories", findMsgByCategories);
            return ResponseResult.build(resp, 200, 10004, "成功搜索商品", jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //回滚事务
            return ResponseResult.fail(resp, "findMoreAboutCategories内部异常");
        }
    }

    @Override
    public ResponseResult findMoreAboutCustomizeCategories(HttpServletResponse resp, JSONObject jsonPram) throws Exception
    {
        JSONObject jsonObject = null;
        String searchInfo = null;
        Integer currPage = null;
        Integer pageSize = null;
        List<MiniProduct> findMsgByCustomizeCategories = new ArrayList<>();
        try {
            jsonObject = new JSONObject();
            searchInfo = jsonPram.getString("searchInfo");
            currPage = jsonPram.getInteger("currPage");
            pageSize = jsonPram.getInteger("pageSize");
            /**输入检验*/
            if(searchInfo == null)
            {
                return ResponseResult.fail(resp,"搜索信息不能为空！");
            }
            /**没有指定页数默认为1*/
            if(currPage == null)
            {
                currPage = 1;
            }
            /**没有指定页大小默认为5*/
            if(pageSize == null)
            {
                pageSize = 5;
            }
            long begin = System.currentTimeMillis();
            long end = System.currentTimeMillis();
            long segmentBegin = System.currentTimeMillis();
            int i = 0;
            /**如果输入的搜索字串长度大于1*/
            if (searchInfo.length() > 1)
            {
                /**根据输入的字符串查找(用户自定义字串部分)
                 * 将输入的字符串拆开来找
                 * 比如%abcde%就拆成%abcde%|%abcd%|%abc|bc%|%b%分别查找
                 * */
                A:
                for (int j = searchInfo.length() - 1; j > i; j--)
                {
                    /**去掉右边一个字符*/
                    List<MiniProduct> temp = productDao.findMsgByCustomizeCategories(searchInfo.substring(i, j), currPage, pageSize);
                    /**找到了相关数据添加*/
                    if (temp.size() != 0 && findMsgByCustomizeCategories.size() < pageSize)
                    {
                        for (Iterator<MiniProduct> iterator = temp.iterator(); iterator.hasNext(); )
                        {
                            MiniProduct p = iterator.next();
                            if (findMsgByCustomizeCategories.size() < pageSize)
                            {

                                /**防止重复数据*/
                                boolean isRepeat = false;
                                for (int z = 0; z < findMsgByCustomizeCategories.size(); z++)
                                {
                                    if (p.getProductId().equals(findMsgByCustomizeCategories.get(z).getProductId()))
                                    {
                                        isRepeat = true;
                                        break;
                                    }
                                }
                                if (!isRepeat)
                                {
                                    findMsgByCustomizeCategories.add(p);
                                }
                            }
                            /**拿到了想要条数的结果就直接退出，节约时间*/
                            else
                            {
                                break A;
                            }
                        }
                    } else if (j > i + 1)
                    {
                        /**去掉左边一个字符*/
                        List<MiniProduct> temp2 = productDao.findMsgByCustomizeCategories(searchInfo.substring(++i, j), currPage, pageSize);
                        /**找到了相关数据添加*/
                        if (temp2.size() != 0 && findMsgByCustomizeCategories.size() < pageSize)
                        {
                            for (MiniProduct p : temp2)
                            {
                                if (findMsgByCustomizeCategories.size() < pageSize)
                                {
                                    /**防止重复数据*/
                                    boolean isRepeat = false;
                                    for (int z = 0; z < findMsgByCustomizeCategories.size(); z++)
                                    {
                                        if (p.getProductId() == findMsgByCustomizeCategories.get(z).getProductId())
                                        {
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                    if (!isRepeat)
                                    {
                                        findMsgByCustomizeCategories.add(p);
                                    }
                                    findMsgByCustomizeCategories.add(p);
                                }
                                /**拿到了想要条数的结果就直接退出，节约时间*/
                                else
                                {
                                    break A;
                                }
                            }
                        }
                        /**拿到空数据直接开始下次循环，省略下面一步判断*/
                        else
                        {
                            continue;
                        }
                    }
                    /**拿到了想要条数的结果就直接退出，节约时间*/
                    if (findMsgByCustomizeCategories.size() >= pageSize)
                    {
                        break A;
                    }

                }
            }
            /**如果用户只输入了一个字符就不用拆分了*/
            else {
                findMsgByCustomizeCategories = productDao.findMsgByCustomizeCategories(searchInfo,currPage,pageSize);
            }
            end = System.currentTimeMillis();
            System.out.println("本次跟据自定义分类查询用时：" + (end - segmentBegin) / 1000 + "." + (end - segmentBegin) % 1000 + "s");
            System.out.println(findMsgByCustomizeCategories);
            jsonObject.put("findMsgByCustomizeCategories", findMsgByCustomizeCategories);
            return ResponseResult.build(resp, 200, 10004, "成功搜索商品", jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //回滚事务
            return ResponseResult.fail(resp, "findMoreAboutCustomizeCategories内部异常");
        }
    }

    @Override
    public ResponseResult findIsFavorite(HttpServletResponse resp, Integer userId, Integer productId) {
        try {
            if (productDao.findIsFavorite(userId, productId) != 0) {
                return ResponseResult.build(resp, 200, 20000, "收藏状态：已收藏", null);
            } else {
                return ResponseResult.build(resp, 200, 20001, "收藏状态：未收藏", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.build(resp, 500, 10009, "获取收藏状态失败", null);
        }

    }

    @Override
    @Transactional
    public ResponseResult switchWhetherToFavorite(HttpServletResponse resp, Integer userId, Integer productId) {
        try {
            /**若已收藏*/
            if (productDao.findIsFavorite(userId, productId) != 0) {
                /**取消收藏*/
                productDao.removeFavorite(userId, productId);
                return ResponseResult.build(resp, 200, 30004, "成功切换收藏状态", null);
            } else {
                /**添加收藏*/
                productDao.addFavorite(userId, productId);
                return ResponseResult.build(resp, 200, 30004, "成功切换收藏状态", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ResponseResult.build(resp, 500, 30005, "切换收藏状态失败", null);
    }

    @Override
    public ResponseResult userReleased(HttpServletResponse resp
            , Integer userId
            , int currentPage
            , int pageSize) {
        try {
            currentPage = (currentPage - 1) * pageSize;
            List<Integer> productsId = productDao.userReleased(userId,currentPage,pageSize);
            if (productsId.size() == 0)
            {
                return ResponseResult.build(resp, 500, 10001, "没有更多页了", null);
            }
            List<MiniProduct> products = productDao.getProductsByIds(productsId);

            return ResponseResult.build(resp, 200, 10022 , "按用户id获取用户发布商品集合成功",products);
        }catch (RuntimeException e)
        {
//            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.build(resp, 500, 10023, "按用户id获取用户发布商品集合失败", null);
        }
    }


    /**
     * 根据用户id获取到我想要的商品
     *
     * @param resp
     * @param userId
     * @return
     */
    @Override
    public ResponseResult getFavoriteProductByUserId(HttpServletResponse resp, Integer userId,Integer currPage,Integer pageSize) {

        try {
            currPage = (currPage-1)*pageSize;
            List<MiniProduct> miniProductList = productDao.getFavoriteProductByUserId(userId,currPage,pageSize);
            if(miniProductList==null||miniProductList.size()==0){
                return ResponseResult.build(resp, 500, 10001, "没有更多页了", null);
            }
            return ResponseResult.build(resp, 200, 10026, "按用户id获取用户收藏商品集合成功", miniProductList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.build(resp, 500, 10027, "按用户id获取用户收藏商品集合失败", null);
        }

    }

    @Override
    public ResponseResult getFavoriteProductCount(HttpServletResponse resp, Integer userId) {
        int favoriteProductCount = 0;
        try {
            favoriteProductCount = productDao.getFavoriteProductCount(userId);
            return ResponseResult.build(resp, 200, 10024, "按用户id获取用户收藏商品数成功", favoriteProductCount);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.build(resp, 500, 10025, "按用户id获取用户收藏商品数失败", null);
        }
    }

    @Override
    public ResponseResult offShelfProduct(HttpServletResponse resp, Integer productId)
    {
        try{
            productDao.offShelfProduct(productId);
            Product product = productDao.findProductById(productId);
            return ResponseResult.build(resp, 200, 30034, "成功下架商品", product);
        }catch (RuntimeException e)
        {
            e.printStackTrace();
            return ResponseResult.build(resp, 500, 30035, "下架商品失败", null);
        }
    }

    @Override
    @Transactional
    public ResponseResult addViewed(HttpServletResponse resp, Integer userId, Integer productId)
    {
        if(productDao.isViewed(userId,productId) != 0)
        {
            return ResponseResult.build(resp, 200, 20005 , "你已经浏览过该商品啦", null);
        }else {
            try
            {
                productDao.addViewed(productId);
//                int i = 1/0;
                productDao.updateIsViewed(userId, productId);
                return ResponseResult.build(resp, 200, 20004 , "商品浏览量+1", null);
            }catch (RuntimeException e)
            {
                e.printStackTrace();
                /**发生异常则回滚事务*/
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return ResponseResult.build(resp, 500, 30024 , "获取今日对该商品的浏览状态失败", null);
    }


}
